from fastapi import HTTPException, APIRouter
from odoo import api, fields, models

from odoo.addons.fastapi_odoo_bridge.core.exception import global_exception_handler

from ..router.common_rt import router as common_router
from ..router.uniapp_rt import router as uniapp_router
from ..router.android_rt import router as android_router
from ..router.html_5_rt import router as html_5_router


class FastapiEndpoint(models.Model):
    _inherit = "fastapi.endpoint"

    app: str = fields.Selection(
        selection_add=[("International API", "内部接口-多语言")], ondelete={"International API": "cascade"}
    )

    def _get_fastapi_routers(self):
        if self.app == "International API":
            # 追加路由并合理分级
            return [common_router, uniapp_router, android_router, html_5_router]
        return super()._get_fastapi_routers()

    def _get_app(self):
        app = super()._get_app()
        if self.app == "International API":
            app.version = "1.0.0"
        return app

    # def _get_app_exception_handlers(self):
    #     handlers = super()._get_app_exception_handlers()
    #     if self.app == "International API":
    #         # 为特定端点添加或覆盖异常处理器
    #         handlers[HTTPException] = global_exception_handler
    #         handlers[Exception] = global_exception_handler
    #     return handlers
